import Vue from 'vue'
import Vuex from 'vuex'
import { MessageBox } from 'mint-ui'

Vue.use(Vuex)
import products from './modules/products.js'
import carts from './modules/carts'
import * as types from './mutation-types.js'
import * as getters from './getters.js'

const store = new Vuex.Store({
  state: {
    headerN: "",
    activeIndex: 0,
    length: 0,
    shopcar: {
      data: [],
      total: 0
    },
    isLogin: false,
    isRegist: false,
    isSelectAll: true,
  },
  modules: {
    products,
    carts
  },
  getters: {
    ...getters,
    totalPrice: state => {
      state.shopcar.data.forEach(x => {
        for (let i in x) {
        }
      })
      let p = state.shopcar.data.reduce((o, n) => {
        // console.log(o.final_price, o.count, n.final_price, n.count)
        let x = n.final_price
        if (!n.isSelected) {
        	x = 0
    		}
        return {

          final_price: o.final_price * o.count + x * n.count,
          count: 1
        }
      }, {
        final_price: 0,
        count: 0
      })
         console.log(p.final_price)
      return p.final_price
    }
  },
  actions: {
    addToCart({ commit }, { goodInfo }) {
      if (goodInfo.inventory > 0) {
        commit(types.ADD_TO_CART, goodInfo);
      }
    }
  },
  mutations: {
  	login(state) {
      state.isLogin = true;
    },
    logout(state) {
      state.isLogin = false;
    },
    regist(state) {
      state.isRegist = true
    },
  	del(state,pid){
  		state.shopcar.data.forEach((x,i) =>{
  			if(x.pid == pid){
  				state.shopcar.data.splice(i,1)
  			}
  		})

  	},

    add(state, pid) {
      state.shopcar.data.forEach(x => {
        if (x.pid == pid) {
          x.count += 1;
        }
      });
    },
    sub(state, pid) {
      state.shopcar.data.forEach(x => {
        if (x.pid == pid) {
          if (x.count > 1) x.count -= 1;
        }
      });
    },
    changeSelected(state, pid) {
    	state.shopcar.data.forEach(x => {
    		if (x.pid == pid) {
    			x.isSelected = !x.isSelected
    			if (!state.shopcar.data.every(x => x.isSelected == true)) {
		    		state.shopcar.isSelectAll = false
		    	} else {
		    		state.shopcar.isSelectAll = true
		    	}
    		}
    	})
    },
    selectAll(state) {
    	if (state.shopcar.data.every(x => x.isSelected == true)) {
    		state.shopcar.data.forEach(x => x.isSelected = false)
    		state.shopcar.isSelectAll = false
    	} else {
    		state.shopcar.data.forEach(x => x.isSelected = true)
    		state.shopcar.isSelectAll = true
    	}
    },
    changeHeadname(state, payload) {
      state.headerN = payload;
    },
    changeIndex(state, payload) {
      state.activeIndex = payload;
    },
    changeBuyCount(state, payload) {
      state.length = payload;
    },
    addToShopcar(state, g) {
      let isFound = false;
      state.shopcar.data.forEach(v => {
        if (v.pid == g.pid) {
          isFound = true;
          v.count += g.count;
        }
      });
      if (!isFound) {
        state.shopcar.data.push(g);
      }
    }
  }
});

export default store;
